#!/bin/sh

export NEWLINE_IFS="$(printf '\n')"

try_function() {
  local E_UNSET=true
  if echo "$-" | grep -q e
  then
    E_UNSET=false
  fi
  "$E_UNSET" || set +e
  (set -e; "$@")
  EXIT_CODE="$?"
  "$E_UNSET" || set -e
  RESULT=false
  if [ "$EXIT_CODE" = 0 ]
  then
    RESULT=true
  fi
}

notrace_function() {
  if eval "[ \"\$E2E_LOG_FUNCTION_$1\" = true ]"
  then
    "$@"
    return
  fi
  local SHELL_XTRACE=""
  local X_UNSET=true
  if echo "$-" | grep -q x
  then
    X_UNSET=false
  fi
  "$X_UNSET" || set +x
  local RESULT
  local EXIT_CODE
  try_function "$@"
  "$X_UNSET" || set -x
  return "$EXIT_CODE"
}

if [ "$E2E_TRACE_ALL" = true ]
then
  [ "${SHELL_XTRACE#-}" != 'x' ] || echo "Everything will be traced!"
  notrace_function() {
    "$@"
  }
fi

transform_params() {
  [ "$#" -ge 2 ]
  local TRANSFORMER="$1"
  local CMD="$2"
  shift 2
  notrace_function do_transform_params "$#" "$TRANSFORMER" "$@" "$CMD"
}

do_transform_params() {
  local SIZE="$1"
  local TRANSFORMER="$2"
  local PARAMETER
  local TRASNFORMED_PARAMETER
  shift 2
  if [ "$SIZE" != 0 ]
  then
    PARAMETER="$1"
    shift 1
    TRASNFORMED_PARAMETER="$("$TRANSFORMER" "$PARAMETER")"
    do_transform_params "$((SIZE - 1))" "$TRANSFORMER" "$@" "$TRASNFORMED_PARAMETER"
  else
    printf "'%s'" "$(escape_quote "$1")"
    shift 1
    for PARAMETER
    do
      printf " '%s'" "$(escape_quote "$PARAMETER")"
    done
  fi
}

escape_quote() {
  printf '%s' "$1" | sed s/\'/\'\\\\\'\'/g
}

export SHELL
export SHELL_XTRACE
SHELL="${E2E_SHELL:-$(readlink /proc/$$/exe || true)}"
if [ "$(basename "$SHELL")" = busybox ] || [ -z "$SHELL" ]
then
  SHELL=sh
fi
SHELL_XTRACE="$(! echo $- | grep -q x || printf '%s' "-x")"

if [ "$E2E_TRACE_ALL" = true ] && [ "x" != "x$SHELL_XTRACE" ]
then
  echo "Everithing will be traced!"
  notrace_function() {
    "$@"
  }
fi

shell() {
  # shellcheck disable=SC2086
  "$SHELL" $SHELL_XTRACE "$@"
}

flock_shell() {
  local LOCK_FILE="$1"
  shift
  # shellcheck disable=SC2086
  flock "$LOCK_FILE" "$SHELL" $SHELL_XTRACE "$@"
}

flock_shared_shell() {
  local LOCK_FILE="$1"
  shift
  # shellcheck disable=SC2086
  flock -s "$LOCK_FILE" "$SHELL" $SHELL_XTRACE "$@"
}

xargs_parallel_shell() {
  [ -n "$1" ]
  local PLACEHOLDER="$1"
  shift
  # shellcheck disable=SC2086
  xargs $([ "x$SHELL_XTRACE" != x-x ] || printf %s -t) -r -I "$PLACEHOLDER" $([ "$E2E_PARALLELISM" -le 1 ] || printf %s "-P $E2E_PARALLELISM") "$SHELL" $SHELL_XTRACE "$@"
}

xargs_shell() {
  [ -n "$1" ]
  local PLACEHOLDER="$1"
  shift
  # shellcheck disable=SC2086
  xargs $([ "x$SHELL_XTRACE" != x-x ] || printf %s -t) -r -I "$PLACEHOLDER" "$SHELL" $SHELL_XTRACE "$@"
}

exec_xargs_parallel_shell() {
  [ -n "$1" ]
  local PLACEHOLDER="$1"
  shift
  # shellcheck disable=SC2086
  exec xargs $([ "x$SHELL_XTRACE" != x-x ] || printf %s -t) -r -I "$PLACEHOLDER" $([ "$E2E_PARALLELISM" -le 1 ] || printf %s "-P $E2E_PARALLELISM") "$SHELL" $SHELL_XTRACE "$@"
}

urlencode() {
  notrace_function traceable_urlencode
}

traceable_urlencode() {
  sed 's/\(.\)/\1\n/g' \
    | {
      NEWLINE="$(printf '\n')"
      while IFS="$NEWLINE" read -r C
      do
        case "$C" in
        [-_.~a-zA-Z0-9]) printf %c "$C" ;;
        "") printf %%0A ;;
        *) printf %%%02X "'$C'" ;;
        esac
      done
      }
}

urldecode() {
  notrace_function traceable_urldecode
}

traceable_urldecode() {
  sed 's/\(.\)/\1\n/g' \
    | {
      NEWLINE="$(printf '\n')"
      CODE=
      while IFS="$NEWLINE" read -r C
      do
        case "$C" in
        \+)
          if [ -n "$CODE" ]
          then
            >&2 echo "Wrong code $CODE$C"
            exit 1
          fi
          printf ' '
          ;;
        %)
          if [ -n "$CODE" ]
          then
            >&2 echo "Wrong code $CODE$C"
            exit 1
          fi
          CODE='0x'
          ;;
        *)
          if [ -z "$CODE" ]
          then
            printf %c "$C"
          else
            CODE="$CODE$C"
            if [ -z "${CODE#0x??}" ]
            then
              # shellcheck disable=SC2059
              printf "$(printf '\\%03o' "$CODE")"
              CODE=
            fi
          fi
          ;;
        esac
      done
      }
}

run_as_root_on_host() {
  "$@"
}

if [ "$K8S_FOR_DIND" = true ] || [ "$(id -u)" != 0 ] || cat /proc/1/cgroup | grep -q ':/docker/'
then
  run_as_root_on_host() {
    docker run --privileged --pid=host -i --rm alpine nsenter -t 1 -m -u -n -i -- "$@"
  }
fi
